package handlers

import (
	"cron-go/configs/error_code"
	"cron-go/internal/pkg/crontab/dao"
	"cron-go/internal/pkg/crontab/models"

	"github.com/go-liam/util/conv"
	"github.com/go-liam/util/response"
)

type GetReadyTask struct {
	Resp []*GetReadyTaskResp
	List []*models.TaskReadyMysqlModel
}

func (s *GetReadyTask) GetData() *response.APIResponse {
	page := &response.Pagination{PageSize: 50000, Current: 1}
	r := &response.ListParameter{WhereSt: " ", OrderSt: " order by id "}
	s.List, _ = new(dao.ReadyDao).FindMulti(page, r)
	s.Change()
	return &response.APIResponse{Code: error_code.Success, Message: error_code.MsSuccess, Data: s.Resp}
}

func (s *GetReadyTask) Change() {
	s.Resp = make([]*GetReadyTaskResp, 0)
	for _, i := range s.List {
		r := new(GetReadyTaskResp)
		r.CreatedAt = i.CreatedAt
		r.Date = conv.StringLeft(i.Date, 10)
		r.ID = i.ID
		r.ServerName = i.ServerName
		r.Status = i.WorkerStatus
		r.TaskID = conv.Int64ToString(i.TaskID)
		r.UpdatedAt = i.UpdatedAt
		s.Resp = append(s.Resp, r)
	}
}

type GetReadyTaskResp struct {
	ID         int64  `json:"id"  `        // ID
	TaskID     string `json:"taskId"  `    // 任务ID
	Date       string `json:"date"  `      //
	CreatedAt  int64  `json:"createdAt" `  // 时间
	UpdatedAt  int64  `json:"updatedAt" `  // 更新时间
	ServerName string `json:"serverName" ` // 名称
	Status     int8   `json:"status" `     // 状态 1:正常 0:停止
	Command    string `json:"command" `    // 执行的命令
}
